package com.winz.cultureapp.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Map;

/**
 * 
 * @Classname: JsonUtil
 * @Description: Json工具，提供对象与json字符串之间的互转
 */
public class JsonUtil {
	
	private static final String TAG = JsonUtil.class.getSimpleName();
	
	/**
	 * 
	 * @Methodname: getArrayFromJson
	 * @Discription: 将json字符串转成数组
	 * @param jsonString
	 * @return
	 *
	 */
	public static Object[] getArrayFromJson(String jsonString) {
		return getGson().fromJson(jsonString, Object[].class);
	}

	
	public static Map<String,String> getMapFromJson(String jsonString) {
		Type type=new TypeToken<Map<String, String>>(){}.getType();
		return getGson().fromJson(jsonString, type);
	}
	
	/**
	 * 
	 * @Methodname: getJsonFromObj
	 * @Discription: 将对象转换成json字符串
	 * @param obj  任意对象
	 * @return
	 *
	 */
	public static String getJsonFromObj(Object obj) {
		return getGson().toJson(obj);
	}
	
	/**
	 * 
	 * @Methodname: getObjFromJson
	 * @Discription: 将json字符串转成对应的对象
	 * @param json  字符串
	 * @param cls  对象类别
	 * @return
	 *
	 */
	public static <T> T getObjFromJson(String json,Class<T> cls) {
		return getGson().fromJson(json, cls);
	}
	
	public static <T> T getObjFromJson(String json,Type type) {
		return getGson().fromJson(json, type);
	}
	
	/**
	 * 
	 * @Methodname: getGson
	 * @Discription: 得到gson对象
	 * @return
	 *
	 */
	public static Gson getGson(){
		return new Gson();
	}

}
